package com.shupian.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON;
import com.shupian.domain.ResponseResult;
import com.shupian.domain.dto.CategoryAddDto;
import com.shupian.domain.entity.Category;
import com.shupian.domain.vo.CategoryAllVo;
import com.shupian.domain.vo.CategoryPageVo;
import com.shupian.domain.vo.ExcelCategoryVo;
import com.shupian.enums.AppHttpCodeEnum;
import com.shupian.service.CategoryService;
import com.shupian.utils.BeanCopyUtils;
import com.shupian.utils.WebUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.util.List;

@RestController
@RequestMapping("/content/category")
@Validated
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 查询所有分类接口 状态正常的
     * @return
     */
    @GetMapping("/listAllCategory")
    public ResponseResult getAllCategory(){
        return categoryService.getAllCategory();
    }

    @PreAuthorize("@ps.hasPermission('content:category:export')")
    @GetMapping("/export")
    public void export(HttpServletResponse response){
        try {
            //设置下载请求头
            WebUtils.setDownLoadHeader("分类.xlsx",response);
            //获取需要导出的数据
            List<Category> categorys = categoryService.list();
            List<ExcelCategoryVo> excelCategoryVos = BeanCopyUtils.copyBeanList(categorys, ExcelCategoryVo.class);
            //把数据写入到Excel中
            EasyExcel.write(response.getOutputStream(), ExcelCategoryVo.class).autoCloseStream(Boolean.FALSE).sheet("分类导出")
                    .doWrite(excelCategoryVos);
        } catch (Exception e) {
            //如果出现异常也要响应json
            ResponseResult responseResult = ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR);
            WebUtils.renderString(response, JSON.toJSONString(responseResult));
        }
    }
    @GetMapping("/list")
    public ResponseResult list(Integer pageNum,Integer pageSize,String name,String status){
        return categoryService.getPageList(pageNum,pageSize,name,status);
    }

    @PostMapping
    public ResponseResult addCategory(@RequestBody CategoryAddDto categoryAddDto){
        return categoryService.addCategory(categoryAddDto);
    }
    @GetMapping("/{id}")
    public ResponseResult getCategoryById(@PathVariable("id") Long id){
        Category category = categoryService.getById(id);
        CategoryPageVo categoryPageVo = BeanCopyUtils.copyBean(category, CategoryPageVo.class);

        return ResponseResult.okResult(categoryPageVo);
    }

    @PutMapping
    public ResponseResult updateCategory(@RequestBody CategoryPageVo categoryPageVo){

        return categoryService.updateCategory(categoryPageVo);
    }
    @DeleteMapping("/{id}")
    public ResponseResult deleteCategory(@PathVariable("id") Long id){
        return categoryService.removeById(id) ? ResponseResult.okResult() : ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR);
    }






}
